package com.mercury.coffee.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mercury.coffee.entity.CoffeeInfoVo;
import com.mercury.coffee.entity.CoffeeWrapper;
import com.mercury.coffee.service.CoffeeInfoService;
import com.mercury.coffee.service.PushCoffeeService;
import com.mercury.coffee.service.PushCoffeeTypeService;
import com.mercury.user.entity.CenterUser;
import com.mercury.user.service.CenterUserService;
import com.mercury.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 童雅婷
 * @since 2021-11-07
 */
@RestController
@RequestMapping("/coffee")
@Api(value = "咖啡豆控制器")
@CrossOrigin
public class PushCoffeeController {

    @Autowired
    private PushCoffeeService coffeeService;

    @Autowired
    private CenterUserService userService;

    private List<CoffeeInfoVo> coffeeVoList;

    /**
     * @return
     * @date 2021/11/7 19:24
     * @params
     * @description 返回咖啡列表，咖啡详情+标签
     */
    @ApiOperation(value = "查询所有咖啡以及他的标签")
    @GetMapping("/coffeeList")
    public R getCoffeeList() {
        coffeeVoList = coffeeService.selectAll();
        return R.ok().data("items", coffeeVoList);
    }

    @ApiOperation(value = "根据 id 查询咖啡的详细信息")
    @GetMapping("coffee/{id}/{userId}")
    public R getCoffee(@PathVariable int id,
                       @PathVariable String userId) {
        CoffeeInfoVo coffeeInfoVo = coffeeService.getCoffeeInfoById(id);
        List<CoffeeInfoVo> wantedCoffee = coffeeService.getWantedCoffee(userId);
        List<CoffeeInfoVo> tasteCoffee = coffeeService.getTasteCoffee(userId);
        CoffeeInfoVo favouriteCoffee = coffeeService.getFavouriteCoffee(userId);
        return R.ok().data("coffeeInfo",coffeeInfoVo)
                .data("iswabted",wantedCoffee.contains(coffeeInfoVo))
                .data("istasted",tasteCoffee.contains(coffeeInfoVo))
                .data("isFavourte",favouriteCoffee.getCoffeeId().equals(id));
    }

    @ApiOperation(value = "获得用户最喜欢的咖啡信息")
    @GetMapping("/favourite/{userId}")
    public R getFavouriteCoffee(@PathVariable String userId) {
        CoffeeInfoVo coffeeInfo = coffeeService.getFavouriteCoffee(userId);
        return R.ok().data("coffeeInfo",coffeeInfo);
    }

    @ApiOperation(value = "用户标记最喜欢的咖啡")
    @PostMapping("/favourite/{id}/{userId}")
    public R setFavouriteCoffee(@PathVariable int id,
                                @PathVariable String userId){
        Boolean result = coffeeService.setFavouriteCoffee(id,userId);
        if(result) return R.ok().message("取消成功");
        else return R.error().message("取消失败");

    }


    @ApiOperation(value = "用户取消标记最喜欢的咖啡")
    @PostMapping("/unFavourite/{id}/{userId}")
    public R cancelFavouriteCoffee(@PathVariable int id,
                                @PathVariable String userId){

        Boolean result = coffeeService.cancelFavouriteCoffee(id,userId);
        if(result) return R.ok().message("取消成功");
        else return R.error().message("取消失败");
    }
    /**
     * @param limit
     * @return
     * @date 2021/11/7 21:58
     * @params current
     * @description 分页显示咖啡豆，但是我是调用了查询所有的咖啡豆的方法，由很大的可优化空间
     * TODO 完善使得该方法更优
     */
    @ApiOperation(value = "分页展示咖啡列表")
    @GetMapping("coffeePage/{current}/{limit}")
    @CrossOrigin
    public R coffeeListPage(@PathVariable long current,
                            @PathVariable long limit) {

        int total = coffeeVoList.size();
        List<CoffeeInfoVo> items = new ArrayList<>();

        for (int i = (int) ((current - 1) * limit); i < (int) current * limit && i < total; i++) {
            items.add(coffeeVoList.get(i));
        }
        return R.ok().data("items", items).data("total", total);
    }


    /**
     * @param coffeeWrapper
     * @return
     * @date 2021/11/9 12:11
     * @params
     * @Param limit
     * @description TODO 完善 ，这里的方法虽然实现功能但是本质上不是做的数据库查询
     */
    @ApiOperation(value = "条件查询")
    @PostMapping("/selectCoffee")
    public R selectCoffeeByType(@RequestBody CoffeeWrapper coffeeWrapper) {

        List<CoffeeInfoVo> res = new ArrayList<>();
        boolean flag = true;
        for (CoffeeInfoVo coffeeInfoVo : coffeeVoList) {
            flag = coffeeInfoVo.getCoffeeName().contains(coffeeWrapper.getName());
            if (!flag) continue;
            for (String type : coffeeWrapper.getTypes()) {
                flag = coffeeInfoVo.getCoffeeTypes().contains(type);
                if (!flag) break;
            }
            if (flag) res.add(coffeeInfoVo);
        }
        return R.ok().data("items", res);
    }

    /**
     * @return
     * @date 2021/11/9 12:12
     * @params coffeeInfoVo
     * @description 向数据库中添加新的咖啡豆
     */
    @Transactional
    @ApiOperation(value = "添加新的咖啡豆")
    @PostMapping("/addCoffee")
    public R addCoffee(@RequestBody CoffeeInfoVo coffeeInfoVo) {
        int id = coffeeService.addCoffee(coffeeInfoVo);
        return R.ok().data("id", id);
    }


    /**
     * @date 2021/11/22 8:27
     * @params userId
     * @return
     * @description 返回用户喝过的咖啡，不放在用户模块是因为会导致互相依赖，因此放在了咖啡模块
    */
    @ApiOperation(value = "获得用户喝过的咖啡")
    @GetMapping("/getTastedCoffee/{userId}")
    public R getTastedCoffee(@PathVariable String userId) {
        List<CoffeeInfoVo> coffeeList = coffeeService.getTasteCoffee(userId);
        return R.ok().data("list",coffeeList);
    }

    @ApiOperation(value = "获得用户想喝的咖啡")
    @GetMapping("/getWantedCoffee/{userId}")
    public R getWantedCoffee(@PathVariable String userId) {
        List<CoffeeInfoVo> coffeeList = coffeeService.getWantedCoffee(userId);
        return R.ok().data("list",coffeeList);
    }

}